﻿using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GameControl;

namespace GameMap
{

    public class BagManager : LogicModuleBase,IBagManager
    {

        string userName;
        public override string Tag => userName;

     //   List<CombinableItemField> ItemsInBag => itemsInBag;

        public List<CombinableItemField> ItemsInBag =>itemsInBag;

        public List<CombinableItemField> ItemsInBench => itemsInBench;

        List<CombinableItemField> itemsInBag = new List<CombinableItemField>();
 
        List<CombinableItemField> itemsInBench = new List<CombinableItemField>();

        public override void Init(object args)
        {
            base.Init(args);
            var tp = args as UserItemPropertyData;
            itemsInBag = tp.ItemsInbag;
            itemsInBench = tp.ItemsInBench;
            userName = tp.UserName;


            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.BagToBench, BagToBench);
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.BenchToBag, BenchToBag);
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.ToBag, ToBag);
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.ChangeTransform, ChangeTransform);
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.DeleteInBench, DeleteItem);

         //   OrginBagLoad();
        }
        EventArgs DeleteItem(object sender, EventArgs args)
        {
            itemsInBench.Remove(itemsInBench.Find(s => s.ItemName == (args as ItemMapArgs).ItemName));
            return null;
        }
        EventArgs ChangeTransform(object sender, EventArgs args)
        {
            if ((args as ItemMapArgs).User != this.userName) return null;
            try
            {

                var item = itemsInBench.Find(s=>s.ItemName==(args as ItemMapArgs).ItemName);
                if (item == null)
                {
                    throw new Exception($"{this}:在该用户下没有找到此物体");
                }
                item.MeshTransform = (args as ItemMapArgs).MeshTransform;
            }
            catch(Exception e)
            {
                Debug.LogError(e);
            }
            return null;
        }
        public class OverlapException : Exception
        {
            public OverlapException(string message):base(message)
            {
            }
        }
        EventArgs BagToBench(object sender,EventArgs args)
        {

            Debug.Assert(args is ItemMapArgs);
            var tp = (args as ItemMapArgs);
           if (this.userName != tp.User) return null;
            try
            {
                var item = itemsInBag.Find(s => s.ItemName == tp.ItemName);
                if (item == null)
                {
                    throw new Exception($"{this}:在该用户下没有找到此物体");
                }
                itemsInBag.Remove(itemsInBag.Find(s=>s.ItemName==tp.ItemName));
                if (itemsInBench.Find(s => s.MeshTransform.BenchMeshPos == item.MeshTransform.BenchMeshPos)!=null)
                {
                    throw (new OverlapException($"{this}:拼装台物体重复"));
                }
                item.IsPlayer = true;
                item.MeshTransform= tp.MeshTransform;
                itemsInBench.Add(item);
                item.BelongingType = ItemBelongingType.Bench;
            }
            catch (OverlapException e)
            {
                Debug.LogWarning(e);
                MessageManager.Instance.Get<ItemMapEvent>().DispatchMessage(ItemMapEvent.ToBag, this, new ItemMapArgs
                {
                    User = userName,
                    ItemName = tp.ItemName
                });
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            return null;
        }
        EventArgs BenchToBag(object sender, EventArgs args)
        {
            Debug.Assert(args is ItemMapArgs);
            var tp = (args as ItemMapArgs);
            if (this.userName!= tp.User) return null;
            try
            {
                itemsInBench.Remove(itemsInBench.Find(s=>s.ItemName==tp.ItemName));
            }
            catch(Exception e)
            {
                Debug.LogError(e);
            }
            return ToBag(sender, args);
        }
        EventArgs ToBag(object sender, EventArgs args)
        {
            Debug.Assert(args is ItemMapArgs);

            var tp = (args as ItemMapArgs);
            if (tp.User != this.userName)
            {
                return null;
            }
            try
            {
                var item = GameManager.Get<IItemInstantiateManager>().GetItemFieldByName(tp.ItemName) as CombinableItemField;
                item.IsPlayer = true;
            //    item.MeshTransform.BenchMeshPos = Vector3Int.zero;
           //     item.MeshTransform.MeshRotate = 0;
           //     item.MeshTransform.IsFliped = false;
                item.BelongingType = ItemBelongingType.Bag;
                item.User = this.userName;
                if (itemsInBag.Contains(item))
                {
                    throw new Exception($"{this}:物体重复出现");
                }
                itemsInBag.Add(item);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            return null;
        }
     
    }

    public interface IBagManager
    {
        List<CombinableItemField> ItemsInBag { get; }
        List<CombinableItemField> ItemsInBench { get; }
    }

}
